/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import database.*;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Scott
 */
public class RosterListPanes {

    Database db;
    private JPanel mainPanel = new JPanel();
    private JPanel adminRosterPanel = new JPanel();
    private JPanel instructorRosterPanel = new JPanel();
    private JPanel studentRosterPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();
    private JButton addUserButton = new JButton("Add User");
    private JButton removeUserButton = new JButton("Remove User");
    private JButton editUserButton = new JButton("Edit User");
    private ArrayList<Administrator> adminArray;
    private ArrayList<Instructor> instArray;
    private ArrayList<Student> studentArray;
    private DefaultListModel adminListModel;
    private DefaultListModel instructorListModel;
    private DefaultListModel studentListModel;
    private JScrollPane adminScrollPane;
    private JScrollPane instScrollPane;
    private JScrollPane studentScrollPane;
    private JLabel adminRosterLabel;
    private JLabel instructorRosterLabel;
    private JLabel studentRosterLabel;

    public RosterListPanes(Database db) {
        this.db = db;
        mainPanel.setLayout(new FlowLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    private void drawButtons() {
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.add(addUserButton);
        buttonPanel.add(removeUserButton);
        buttonPanel.add(editUserButton);

    }

    public JPanel getAdminPanel() {
        drawAdminPane();
        return mainPanel;
    }

    public JPanel getInstPanel() {
        drawInstructorPane();
        return mainPanel;
    }

    public JPanel getStudentPanel() {
        drawStudentPane();
        return mainPanel;
    }

    public void updateAdminPane() {

        adminRosterPanel.setVisible(false);
        adminListModel.clear();
        adminRosterPanel.remove(adminRosterLabel);
        adminRosterPanel.remove(adminScrollPane);
        drawAdminPane();

    }

    public void updateInstPane() {

        instructorRosterPanel.setVisible(false);
        instructorListModel.clear();
        instructorRosterPanel.remove(instructorRosterLabel);
        instructorRosterPanel.remove(instScrollPane);
        drawInstructorPane();

    }

    public void updateStudentPane() {

        studentRosterPanel.setVisible(false);
        studentListModel.clear();
        studentRosterPanel.remove(studentRosterLabel);
        studentRosterPanel.remove(studentScrollPane);
        drawStudentPane();

    }

    public void fillAdminPane() {

        adminListModel = new DefaultListModel();
        adminArray = db.getAdmins();
        //fill list from array

        for (int a = 0; a < adminArray.size(); a++) {
            adminListModel.addElement(adminArray.get(a).getID());
        }
    }

    public void fillInstPane() {

        instructorListModel = new DefaultListModel();
        instArray = db.getInstructors();
        //fill list from array

        for (int a = 0; a < instArray.size(); a++) {
            instructorListModel.addElement(instArray.get(a).getID());
        }
    }

    public void fillStudentPane() {

        studentListModel = new DefaultListModel();
        studentArray = db.getStudents();
        //fill list from array

        for (int a = 0; a < studentArray.size(); a++) {
            studentListModel.addElement(studentArray.get(a).getID());
        }
    }

    private void drawAdminPane() {

        adminRosterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        adminRosterPanel.setLayout(new BoxLayout(adminRosterPanel, BoxLayout.Y_AXIS));

        adminRosterLabel = new JLabel("Administrator Roster");

        fillAdminPane();
        drawButtons();

        JList adminList = new JList(adminListModel);
        adminList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //        adminList.addListSelectionListener(new MyListListener(adminList, removeUserButton, editUserButton));
        adminList.setVisibleRowCount(20);
        adminScrollPane = new JScrollPane(adminList);

        adminRosterPanel.add(adminRosterLabel);
        adminRosterPanel.add(adminScrollPane);

        mainPanel.add(adminRosterPanel);
        mainPanel.add(buttonPanel);

    }

    private void drawInstructorPane() {

        instructorRosterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        instructorRosterPanel.setLayout(new BoxLayout(instructorRosterPanel, BoxLayout.Y_AXIS));


        fillInstPane();
        drawButtons();


        JList iList = new JList(instructorListModel);
        iList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //adminList.addListSelectionListener(new MyListListener(adminList, removeUserButton, editUserButton));
        iList.setVisibleRowCount(20);

        instructorRosterLabel = new JLabel("Instructor Roster");
        instScrollPane = new JScrollPane(iList);

        instructorRosterPanel.add(instructorRosterLabel);
        instructorRosterPanel.add(instScrollPane);

        mainPanel.add(instructorRosterPanel);
        mainPanel.add(buttonPanel);

    }

    private void drawStudentPane() {

        studentRosterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        studentRosterPanel.setLayout(new BoxLayout(studentRosterPanel, BoxLayout.Y_AXIS));

        fillStudentPane();
        drawButtons();

        JList sList = new JList(studentListModel);
        sList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //adminList.addListSelectionListener(new MyListListener(adminList, removeUserButton, editUserButton));
        sList.setVisibleRowCount(20);

        studentRosterLabel = new JLabel("Student Roster");
        studentScrollPane = new JScrollPane(sList);

        studentRosterPanel.add(studentRosterLabel);
        studentRosterPanel.add(studentScrollPane);

        mainPanel.add(studentRosterPanel);
        mainPanel.add(buttonPanel);

    }
}//end class RosterListPanes
